<template>
  <div class="layout-container">
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="header-left">
        <h2>房屋租赁系统 - 管理员平台</h2>
      </div>
      <div class="header-right">
        <span>欢迎您，管理员 {{ username }}</span>
      </div>
    </el-header>

    <el-container class="main-container">
      <!-- 左侧导航栏 -->
      <el-aside width="250px" class="aside">
        <!-- 用户信息卡片 -->
        <div class="user-info">
          <el-avatar :size="64" :src="userAvatar">
            <img src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"/>
          </el-avatar>
          <h3>{{ username }}</h3>
          <el-button type="danger" size="small" @click="handleLogout">退出登录</el-button>
        </div>

        <el-menu
          :default-active="activeMenu"
          class="menu"
          background-color="#304156"
          text-color="#fff"
          active-text-color="#409EFF"
          router
        >
          <el-menu-item index="/admin/house">
            <el-icon><House /></el-icon>
            <span>房屋管理</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/user">
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/finance">
            <el-icon><Money /></el-icon>
            <span>财政统计</span>
          </el-menu-item>
          
          <el-menu-item index="/admin/feedback">
            <el-icon><ChatDotRound /></el-icon>
            <span>意见反馈</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主要内容区域 -->
      <el-main class="main">
        <router-view />
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import { User, House, Money, ChatDotRound } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

// 从localStorage获取用户名
const username = ref('')
const userAvatar = ref('')

// 计算当前激活的菜单项
const activeMenu = computed(() => {
  return route.path
})

onMounted(() => {
  // 获取登录时存储的用户信息
  username.value = localStorage.getItem('username') || '管理员'
  userAvatar.value = localStorage.getItem('userAvatar') || ''
})

// 处理退出登录
const handleLogout = () => {
  ElMessageBox.confirm(
    '确定要退出登录吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // 清除本地存储的信息
    localStorage.removeItem('token')
    localStorage.removeItem('userId')
    localStorage.removeItem('isAdmin')
    localStorage.removeItem('username')
    localStorage.removeItem('userAvatar')
    
    ElMessage.success('已成功退出登录')
    router.push('/login')
  }).catch(() => {
    // 取消退出登录
  })
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  background-color: #f0f2f5;
}

.header {
  background-color: #409EFF;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
}

.header-left h2 {
  color: #fff;
  margin: 0;
}

.header-right span {
  color: #fff;
}

.main-container {
  height: calc(100vh - 60px);
}

.aside {
  background-color: #304156;
  height: 100%;
}

.menu {
  border-right: none;
}

.user-info {
  padding: 20px;
  text-align: center;
  background-color: #263445;
  color: #fff;
}

.user-info h3 {
  margin: 10px 0;
  font-size: 16px;
  color: #fff;
}

.user-info .el-button {
  margin-top: 10px;
}

.main {
  padding: 20px;
  background-color: #f0f2f5;
}
</style>